import { defineComponent, ref, reactive, toValue, watch, nextTick, onMounted, openBlock, createElementBlock, createElementVNode, renderSlot, normalizeStyle, createCommentVNode } from "vue";
const _hoisted_1 = { class: "me-cursor-follow" };
const _hoisted_2 = ["innerHTML"];
const _sfc_main = /* @__PURE__ */ defineComponent({
  ...{
    name: "me-cursor-follow"
  },
  __name: "cursor-follow",
  props: {
    cursorStyle: { default() {
      return {};
    } },
    content: { default: "" },
    scrollToBottom: { type: Boolean }
  },
  setup(__props, { expose: __expose }) {
    const props = __props;
    const boxRef = ref();
    const showCursor = ref(false);
    const pointRef = ref();
    let lastTextNode = null;
    const position = reactive({
      x: 0,
      y: 0
    });
    const getLastTextNode = (target) => {
      const dom = toValue(target);
      if (dom) {
        if (dom.nodeType === Node.TEXT_NODE && dom.nodeValue) {
          return dom;
        }
        for (let i = dom.childNodes.length - 1; i >= 0; i--) {
          const textNode = getLastTextNode(dom.childNodes[i]);
          if (textNode) {
            return textNode;
          }
        }
      }
      return null;
    };
    const updateCursor = () => {
      var _a;
      lastTextNode = getLastTextNode(boxRef.value);
      const text = document.createTextNode("|");
      if (lastTextNode) {
        lastTextNode.after(text);
      } else {
        boxRef.value.appendChild(text);
      }
      const range = document.createRange();
      range.setStart(text, 0);
      range.setEnd(text, 0);
      const rangeRect = range.getBoundingClientRect();
      const targetRect = boxRef.value.getBoundingClientRect();
      const cursorRect = pointRef.value ? pointRef.value.getBoundingClientRect() : { width: 0 };
      position.x = rangeRect.left - targetRect.left;
      position.y = rangeRect.top - targetRect.top + rangeRect.height / 2 - cursorRect.width / 2;
      (_a = text.parentNode) == null ? void 0 : _a.removeChild(text);
    };
    watch(() => props.content, () => {
      nextTick(() => {
        updateCursor();
      });
    });
    const removeCursor = () => {
      showCursor.value = false;
    };
    const addCursor = () => {
      showCursor.value = true;
    };
    onMounted(() => {
      nextTick(() => {
        updateCursor();
      });
    });
    __expose({
      updateCursor,
      removeCursor,
      addCursor
    });
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", _hoisted_1, [
        createElementVNode("div", {
          ref_key: "boxRef",
          ref: boxRef,
          class: "me-cursor-follow-content",
          innerHTML: _ctx.content
        }, null, 8, _hoisted_2),
        renderSlot(_ctx.$slots, "default"),
        showCursor.value ? (openBlock(), createElementBlock("div", {
          key: 0,
          ref_key: "pointRef",
          ref: pointRef,
          class: "me-cursor-follow-point",
          style: normalizeStyle({ ..._ctx.cursorStyle, left: `${position.x}px`, top: `${position.y}px` })
        }, null, 4)) : createCommentVNode("", true)
      ]);
    };
  }
});
export {
  _sfc_main as default
};
//# sourceMappingURL=cursor-follow.vue2.mjs.map
